package com.zh.edu.info.manager.dao;

import com.zh.edu.info.manager.domain.Student;

public class StudentDao implements BaseStudentDao{
    private static Student[] arr = new Student[5];

    static {
        Student s1 = new Student("1", "小张", 25, "1997");
        Student s2 = new Student("2", "小王", 23, "1999");

        arr[0] = s1;
        arr[1] = s2;
    }

    @Override
    public boolean addStudent(Student student) {
        int index = -1;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            return false;
        } else {
            arr[index] = student;
            return true;
        }

    }

    @Override
    public Student[] findAll() {
        return arr;
    }

    @Override
    public void setStudent(String id, Student student) {
        int index = getIndex(id);
        arr[index] = student;
    }

    @Override
    public void selectStudent(String id) {
        int index = getIndex(id);
        arr[index] = null;
    }

    @Override
    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null && arr[i].getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

}
